<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>荣誉证书识别系统</title>
    <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.4.120/pdf.min.js"></script>
    <script>
        pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.4.120/pdf.worker.min.js';
    </script>
    <style>
        :root {
            --primary-color: #4361ee;
            --secondary-color: #3f37c9;
            --accent-color: #4895ef;
            --light-color: #f8f9fa;
            --dark-color: #212529;
            --success-color: #4cc9f0;
            --border-radius: 16px;
            --box-shadow: 0 12px 24px rgba(0, 0, 0, 0.08);
            --transition: all 0.3s ease;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Noto Sans SC', sans-serif;
            background: linear-gradient(135deg, #f0f4ff 0%, #e6e9ff 100%);
            min-height: 100vh;
            padding: 2rem;
            color: var(--dark-color);
            line-height: 1.6;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 20px;
        }

        header {
            text-align: center;
            margin-bottom: 3rem;
            padding-top: 1rem;
        }

        h1 {
            font-size: 2.8rem;
            font-weight: 700;
            background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
            -webkit-background-clip: text;
            background-clip: text;
            -webkit-text-fill-color: transparent;
            margin-bottom: 1.2rem;
            position: relative;
            display: inline-block;
        }

        h1::after {
            content: '';
            position: absolute;
            bottom: -12px;
            left: 50%;
            transform: translateX(-50%);
            width: 100px;
            height: 5px;
            background: linear-gradient(to right, var(--accent-color), var(--success-color));
            border-radius: 3px;
        }

        .subtitle {
            font-size: 1.2rem;
            color: #6c757d;
            max-width: 700px;
            margin: 0 auto;
            padding: 0 20px;
        }

        .card {
            background: rgba(255, 255, 255, 0.95);
            border-radius: var(--border-radius);
            box-shadow: var(--box-shadow);
            padding: 2.5rem;
            margin-bottom: 2.5rem;
            transition: var(--transition);
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.5);
        }

        .card:hover {
            transform: translateY(-8px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
        }

        .upload-container {
            border: 2px dashed #c0c5ff;
            border-radius: var(--border-radius);
            padding: 3.5rem 2rem;
            text-align: center;
            cursor: pointer;
            transition: var(--transition);
            position: relative;
            overflow: hidden;
            background: rgba(248, 249, 250, 0.6);
        }

        .upload-container:hover {
            border-color: var(--primary-color);
            background: rgba(67, 97, 238, 0.08);
        }

        .upload-container.dragover {
            border-color: var(--primary-color);
            background: rgba(67, 97, 238, 0.15);
            animation: pulse 1.5s infinite;
        }

        @keyframes pulse {
            0% { box-shadow: 0 0 0 0 rgba(67, 97, 238, 0.2); }
            70% { box-shadow: 0 0 0 15px rgba(67, 97, 238, 0); }
            100% { box-shadow: 0 0 0 0 rgba(67, 97, 238, 0); }
        }

        .upload-icon {
            font-size: 4rem;
            color: var(--primary-color);
            margin-bottom: 1.5rem;
            transition: var(--transition);
        }

        .upload-text {
            font-size: 1.3rem;
            margin-bottom: 1.8rem;
            color: #495057;
            font-weight: 500;
        }

        .btn {
            display: inline-block;
            background: linear-gradient(to right, var(--primary-color), var(--secondary-color));
            color: white;
            border: none;
            padding: 1rem 2.2rem;
            border-radius: 50px;
            cursor: pointer;
            font-size: 1.1rem;
            font-weight: 500;
            transition: var(--transition);
            box-shadow: 0 6px 12px rgba(50, 50, 93, 0.15);
            position: relative;
            overflow: hidden;
        }

        .btn::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(255, 255, 255, 0.1);
            transform: translateX(-100%);
            transition: transform 0.5s ease;
        }

        .btn:hover {
            transform: translateY(-3px);
            box-shadow: 0 10px 20px rgba(50, 50, 93, 0.2);
        }

        .btn:hover::before {
            transform: translateX(100%);
        }

        .btn i {
            margin-right: 8px;
        }

        #fileInput {
            display: none;
        }

        .preview-container {
            display: grid;
            grid-template-columns: 1fr;
            gap: 2.5rem;
            margin-bottom: 2.5rem;
        }

        .preview-card {
            background: rgba(255, 255, 255, 0.95);
            border-radius: var(--border-radius);
            overflow: hidden;
            box-shadow: var(--box-shadow);
            height: 520px;
            transition: var(--transition);
        }

        .preview-card:hover {
            transform: translateY(-5px);
        }

        .preview-header {
            padding: 1.2rem;
            background: linear-gradient(to right, var(--primary-color), var(--secondary-color));
            color: white;
            font-weight: 500;
            display: flex;
            align-items: center;
            font-size: 1.2rem;
        }

        .preview-header i {
            margin-right: 12px;
            font-size: 1.3rem;
        }

        .preview-body {
            padding: 1.5rem;
            height: calc(100% - 56px);
            display: flex;
            align-items: center;
            justify-content: center;
            overflow: auto;
            background: #f9faff;
        }

        #previewImage {
            max-width: 100%;
            max-height: 100%;
            display: block;
            margin: 0 auto;
            border-radius: 10px;
            box-shadow: 0 6px 15px rgba(0,0,0,0.1);
            border: 1px solid #e0e7ff;
        }

        .no-preview {
            text-align: center;
            padding: 2rem;
            color: #adb5bd;
            height: 100%;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
        }

        .no-preview i {
            font-size: 4rem;
            margin-bottom: 1.5rem;
            color: #d0d5ff;
        }

        .no-preview p {
            max-width: 300px;
            margin: 0 auto;
        }

        .result-container {
            background: rgba(255, 255, 255, 0.95);
            border-radius: var(--border-radius);
            overflow: hidden;
            box-shadow: var(--box-shadow);
            transition: var(--transition);
        }

        .result-container:hover {
            transform: translateY(-5px);
        }

        .result-header {
            padding: 1.4rem;
            background: linear-gradient(to right, var(--primary-color), var(--secondary-color));
            color: white;
            font-size: 1.4rem;
            font-weight: 500;
            display: flex;
            align-items: center;
        }

        .result-header i {
            margin-right: 12px;
            font-size: 1.5rem;
        }

        .result-body {
            padding: 2rem 1.5rem;
        }

        .result-item {
            display: flex;
            margin-bottom: 1.5rem;
            padding-bottom: 1.5rem;
            border-bottom: 1px solid #e9ecef;
            transition: var(--transition);
        }

        .result-item:hover {
            background: rgba(248, 249, 250, 0.5);
            border-radius: 8px;
            padding: 10px;
            transform: translateX(5px);
        }

        .result-item:last-child {
            border-bottom: none;
            margin-bottom: 0;
            padding-bottom: 0;
        }

        .result-label {
            flex: 0 0 140px;
            font-weight: 600;
            color: var(--dark-color);
            display: flex;
            align-items: center;
            font-size: 1.1rem;
        }

        .result-label i {
            margin-right: 1rem;
            color: var(--accent-color);
            font-size: 1.3rem;
            width: 30px;
            text-align: center;
        }

        .result-value {
            flex: 1;
            color: #495057;
            padding: 0.8rem 1.2rem;
            background: #f8f9fa;
            border-radius: 10px;
            min-height: 50px;
            display: flex;
            align-items: center;
            font-size: 1.1rem;
            border-left: 3px solid var(--accent-color);
            transition: var(--transition);
        }

        .loading {
            text-align: center;
            padding: 3rem;
            display: none;
            background: rgba(255, 255, 255, 0.9);
            border-radius: var(--border-radius);
            margin: 2rem 0;
            box-shadow: var(--box-shadow);
        }

        .spinner {
            width: 60px;
            height: 60px;
            margin: 0 auto 1.8rem;
            border: 5px solid rgba(67, 97, 238, 0.2);
            border-radius: 50%;
            border-top-color: var(--primary-color);
            animation: spin 1.2s linear infinite;
        }

        @keyframes spin {
            to { transform: rotate(360deg); }
        }

        .loading-text {
            font-size: 1.2rem;
            color: #495057;
            font-weight: 500;
        }

        footer {
            text-align: center;
            margin-top: 4rem;
            color: #6c757d;
            font-size: 1rem;
            padding: 1.5rem 0;
            border-top: 1px solid rgba(0,0,0,0.05);
        }

        .feature-section {
            display: flex;
            justify-content: center;
            flex-wrap: wrap;
            margin-bottom: 3rem;
            gap: 25px;
        }

        .feature-card {
            background: rgba(255, 255, 255, 0.95);
            border-radius: var(--border-radius);
            box-shadow: var(--box-shadow);
            padding: 2rem 1.5rem;
            text-align: center;
            width: 220px;
            margin: 0.5rem;
            transition: var(--transition);
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.5);
        }

        .feature-card:hover {
            transform: translateY(-8px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.12);
        }

        .feature-icon {
            font-size: 2.5rem;
            color: var(--primary-color);
            margin-bottom: 1.5rem;
            height: 70px;
            width: 70px;
            line-height: 70px;
            border-radius: 50%;
            background: rgba(67, 97, 238, 0.1);
            margin: 0 auto 1.5rem;
            transition: var(--transition);
        }

        .feature-card:hover .feature-icon {
            transform: scale(1.1);
            background: rgba(67, 97, 238, 0.2);
        }

        .feature-title {
            font-weight: 700;
            margin-bottom: 0.8rem;
            font-size: 1.2rem;
            color: var(--secondary-color);
        }

        .feature-desc {
            font-size: 0.95rem;
            color: #6c757d;
            line-height: 1.5;
        }

        .file-info {
            margin-top: 15px;
            font-size: 1rem;
            color: #6c757d;
            text-align: center;
        }

        .pdf-preview-container {
            width: 100%;
            height: 100%;
            overflow: auto;
            padding: 10px;
        }

        .pdf-preview-container canvas {
            max-width: 100%;
            border-radius: 10px;
            box-shadow: 0 6px 15px rgba(0,0,0,0.1);
            border: 1px solid #e0e7ff;
        }

        .status-message {
            padding: 12px 20px;
            border-radius: 8px;
            margin-top: 15px;
            text-align: center;
            font-weight: 500;
            display: none;
            max-width: 500px;
            margin: 15px auto 0;
            font-size: 1rem;
        }

        .status-success {
            background-color: rgba(76, 201, 240, 0.15);
            color: #0d5a70;
            border: 1px solid rgba(76, 201, 240, 0.3);
        }

        .status-error {
            background-color: rgba(248, 215, 218, 0.3);
            color: #721c24;
            border: 1px solid rgba(220, 53, 69, 0.2);
        }

        @media (max-width: 768px) {
            body {
                padding: 1.5rem 1rem;
            }

            .preview-container {
                grid-template-columns: 1fr;
            }

            .preview-card {
                height: 400px;
            }

            .result-item {
                flex-direction: column;
            }

            .result-label {
                margin-bottom: 0.8rem;
                flex: 0 0 auto;
            }

            .feature-section {
                flex-direction: column;
                align-items: center;
            }

            .feature-card {
                width: 100%;
                max-width: 350px;
                margin-bottom: 1.5rem;
            }

            h1 {
                font-size: 2.2rem;
            }

            .subtitle {
                font-size: 1rem;
            }

            .card {
                padding: 1.8rem;
            }

            .upload-container {
                padding: 2.5rem 1.5rem;
            }

            .upload-text {
                font-size: 1.1rem;
            }
        }
    </style>
</head>
<body>
<div class="container">
    <header>
        <h1>荣誉证书识别系统</h1>
        <p class="subtitle">上传荣誉证书图片或PDF，自动识别获奖人、指导老师、奖项和发证单位</p>
    </header>

    <div class="feature-section">
        <div class="feature-card">
            <div class="feature-icon">
                <i class="fas fa-bolt"></i>
            </div>
            <h3 class="feature-title">快速识别</h3>
            <p class="feature-desc">利用先进OCR技术，快速提取证书关键信息</p>
        </div>

        <div class="feature-card">
            <div class="feature-icon">
                <i class="fas fa-file-pdf"></i>
            </div>
            <h3 class="feature-title">多格式支持</h3>
            <p class="feature-desc">支持JPG、PNG图片及PDF文档识别</p>
        </div>

        <div class="feature-card">
            <div class="feature-icon">
                <i class="fas fa-shield-alt"></i>
            </div>
            <h3 class="feature-title">数据安全</h3>
            <p class="feature-desc">文件仅在本地处理，不上传至服务器</p>
        </div>

        <div class="feature-card">
            <div class="feature-icon">
                <i class="fas fa-trophy"></i>
            </div>
            <h3 class="feature-title">精准解析</h3>
            <p class="feature-desc">智能识别各类证书版式，准确提取信息</p>
        </div>
    </div>

    <div class="card">
        <div class="upload-container" id="dropArea">
            <div class="upload-icon">
                <i class="fas fa-cloud-upload-alt"></i>
            </div>
            <div class="upload-text">拖放文件到此处或点击按钮选择文件</div>
            <button class="btn" id="uploadBtn">
                <i class="fas fa-folder-open"></i> 选择文件
            </button>
            <input type="file" id="fileInput" accept="image/*,application/pdf">
            <div class="file-info">支持格式：JPG, PNG, PDF (最大10MB)</div>
            <div id="statusMessage" class="status-message"></div>
        </div>
    </div>

    <div class="preview-container">
        <div class="preview-card">
            <div class="preview-header">
                <i class="fas fa-file-image"></i> 文件预览
            </div>
            <div class="preview-body">
                <img id="previewImage" src="" alt="预览图" style="display: none;">
                <div id="pdfPreview" class="pdf-preview-container" style="display: none;"></div>
                <div class="no-preview" id="noPreviewText">
                    <i class="far fa-image"></i>
                    <p>请拖放文件到上传区域或点击"选择文件"按钮</p>
                    <p style="font-size: 0.95rem; margin-top: 10px; color: #8a92b0;">支持图片和PDF格式</p>
                </div>
            </div>
        </div>
    </div>

    <div class="loading" id="loadingIndicator">
        <div class="spinner"></div>
        <p class="loading-text">正在识别证书内容，请稍候...</p>
    </div>

    <div class="result-container" id="resultContainer" style="display: none;">
        <div class="result-header">
            <i class="fas fa-award"></i> 识别结果
        </div>
        <div class="result-body">
            <div class="result-item">
                <div class="result-label">
                    <i class="fas fa-user-graduate"></i> 获奖人
                </div>
                <div class="result-value" id="student">-</div>
            </div>
            <div class="result-item">
                <div class="result-label">
                    <i class="fas fa-chalkboard-teacher"></i> 指导老师
                </div>
                <div class="result-value" id="teacher">-</div>
            </div>
            <div class="result-item">
                <div class="result-label">
                    <i class="fas fa-trophy"></i> 奖项
                </div>
                <div class="result-value" id="competition">-</div>
            </div>
            <div class="result-item">
                <div class="result-label">
                    <i class="fas fa-university"></i> 发证单位
                </div>
                <div class="result-value" id="enterprise">-</div>
            </div>
            <div class="result-item">
                <div class="result-label">
                    <i class="fas fa-calendar-alt"></i> 发证日期
                </div>
                <div class="result-value" id="date">-</div>
            </div>
            <div class="result-item">
                <div class="result-label">
                    <i class="fas fa-hashtag"></i> 证书编号
                </div>
                <div class="result-value" id="number">-</div>
            </div>
        </div>
    </div>

    <footer>
        <p>© 2025 荣誉证书识别系统 | 优化版 @北辰yuan</p>
    </footer>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        const dropArea = document.getElementById('dropArea');
        const uploadBtn = document.getElementById('uploadBtn');
        const fileInput = document.getElementById('fileInput');
        const previewImage = document.getElementById('previewImage');
        const pdfPreview = document.getElementById('pdfPreview');
        const noPreviewText = document.getElementById('noPreviewText');
        const loadingIndicator = document.getElementById('loadingIndicator');
        const resultContainer = document.getElementById('resultContainer');
        const statusMessage = document.getElementById('statusMessage');

        // 结果字段
        const student = document.getElementById('student');
        const teacher = document.getElementById('teacher');
        const competition = document.getElementById('competition');
        const enterprise = document.getElementById('enterprise');
        const date = document.getElementById('date');
        const number = document.getElementById('number');

        let isProcessing = false;

        function showStatus(message, isSuccess = true) {
            statusMessage.textContent = message;
            statusMessage.className = `status-message ${isSuccess ? 'status-success' : 'status-error'}`;
            statusMessage.style.display = 'block';

            setTimeout(() => {
                statusMessage.style.display = 'none';
            }, 4000);
        }

        ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
            dropArea.addEventListener(eventName, preventDefaults, false);
        });

        function preventDefaults(e) {
            e.preventDefault();
            e.stopPropagation();
        }

        ['dragenter', 'dragover'].forEach(eventName => {
            dropArea.addEventListener(eventName, highlight, false);
        });

        ['dragleave', 'drop'].forEach(eventName => {
            dropArea.addEventListener(eventName, unhighlight, false);
        });

        function highlight() {
            dropArea.classList.add('dragover');
        }

        function unhighlight() {
            dropArea.classList.remove('dragover');
        }

        dropArea.addEventListener('drop', handleDrop, false);

        function handleDrop(e) {
            const dt = e.dataTransfer;
            const files = dt.files;
            if (files.length) {
                handleFiles(files);
            }
        }

        uploadBtn.addEventListener('click', () => {
            if (!isProcessing) {
                fileInput.click();
            } else {
                showStatus('系统正在处理文件，请稍候...', false);
            }
        });

        fileInput.addEventListener('change', function() {
            if (this.files.length && !isProcessing) {
                handleFiles(this.files);
            }
        });

        function handleFiles(files) {
            if (isProcessing) {
                showStatus('请等待当前文件处理完成', false);
                return;
            }

            isProcessing = true;
            const file = files[0];
            const fileType = file.type;
            const validImageTypes = ['image/jpeg', 'image/png', 'image/jpg'];
            const maxSize = 10 * 1024 * 1024; // 10MB

            resetPreview();

            if (file.size > maxSize) {
                showStatus('文件大小超过限制（最大10MB）', false);
                isProcessing = false;
                return;
            }

            if (validImageTypes.includes(fileType)) {
                const reader = new FileReader();
                reader.onload = function(e) {
                    previewImage.src = e.target.result;
                    previewImage.style.display = 'block';
                    noPreviewText.style.display = 'none';
                    showStatus('图片上传成功，正在识别...', true);
                    sendFileToBackend(file);
                };
                reader.readAsDataURL(file);
            } else if (fileType === 'application/pdf') {
                renderPdfFirstPage(file);
            } else {
                showStatus('请选择图片文件 (JPEG, PNG) 或 PDF 文件', false);
                isProcessing = false;
            }
        }

        function resetPreview() {
            previewImage.style.display = 'none';
            previewImage.src = '';
            pdfPreview.style.display = 'none';
            pdfPreview.innerHTML = '';
            noPreviewText.style.display = 'flex';
            resultContainer.style.display = 'none';
        }

        async function renderPdfFirstPage(file) {
            loadingIndicator.style.display = 'block';

            try {
                const arrayBuffer = await file.arrayBuffer();
                const typedArray = new Uint8Array(arrayBuffer);

                const pdfDoc = await pdfjsLib.getDocument(typedArray).promise;
                const page = await pdfDoc.getPage(1);
                const viewport = page.getViewport({ scale: 1.8 });

                const canvas = document.createElement('canvas');
                const context = canvas.getContext('2d');
                canvas.height = viewport.height;
                canvas.width = viewport.width;

                await page.render({
                    canvasContext: context,
                    viewport: viewport
                }).promise;

                pdfPreview.innerHTML = '';
                pdfPreview.appendChild(canvas);
                pdfPreview.style.display = 'block';
                noPreviewText.style.display = 'none';

                showStatus('PDF预览成功，正在识别...', true);
                sendFileToBackend(file);
            } catch (error) {
                console.error('PDF渲染错误:', error);
                showStatus('PDF渲染失败: ' + error.message, false);
                noPreviewText.style.display = 'flex';
                loadingIndicator.style.display = 'none';
                isProcessing = false;
            }
        }
        function sendFileToBackend(file) {
            loadingIndicator.style.display = 'block';
            resultContainer.style.display = 'none';

            const formData = new FormData();
            formData.append('file', file);

            fetch('/api/recognize-certificate', {
                method: 'POST',
                body: formData
            })
                .then(response => {
                    if (!response.ok) {
                        throw new Error('网络响应不正常');
                    }
                    return response.json();
                })
                .then(data => {
                    console.log("后端返回的数据:", data);
                    document.getElementById('student').textContent = data.student || '-';
                    document.getElementById('teacher').textContent = data.teacher || '-';
                    document.getElementById('competition').textContent = data.competition || '-';
                    document.getElementById('enterprise').textContent = data.enterprise || '-';
                    document.getElementById('date').textContent = data.date || '-';
                    document.getElementById('number').textContent = data.number || '-';

                    resultContainer.style.display = 'block';
                    loadingIndicator.style.display = 'none';
                    isProcessing = false;
                    showStatus('识别成功！', true);
                })
                .catch(error => {
                    console.error('识别失败:', error);
                    showStatus('识别失败，请重试', false);
                    loadingIndicator.style.display = 'none';
                    isProcessing = false;
                });
        }


        // 添加CSS动画
        const style = document.createElement('style');
        style.textContent = `
            @keyframes fadeIn {
                from { opacity: 0; transform: translateY(20px); }
                to { opacity: 1; transform: translateY(0); }
            }
            .result-container {
                animation: fadeIn 0.8s ease;
            }
        `;
        document.head.appendChild(style);
    });
</script>
</body>
</html>